/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.test.internal.performance.db;

import java.io.PrintStream;
import java.util.HashMap;

public class Report {

    private static final String LEFT       = "l";          //$NON-NLS-1$
    private static final String RIGHT      = "r";          //$NON-NLS-1$

    private int                 fGap;
    private int                 fColumn;
    private int                 fRow;
    private int                 fRows;
    private HashMap<String, String>             fContent   = new HashMap<>();
    private HashMap<String, Integer>             fWidths    = new HashMap<>();
    private HashMap<String, String>             fAlignment = new HashMap<>();

    public Report(int gap) {
        fGap = gap;
    }

    public void addCell(String value) {
        setCell(fColumn, fRow, value, LEFT);
        fColumn++;
    }

    public void addCellRight(String value) {
        setCell(fColumn, fRow, value, RIGHT);
        fColumn++;
    }

    public void nextRow() {
        fRow++;
        fColumn = 0;
    }

    private void setCell(int x, int y, String value, String align) {
        fContent.put(String.valueOf(x + "/" + y), value); //$NON-NLS-1$
        fAlignment.put(String.valueOf(x + "/" + y), align); //$NON-NLS-1$
        Integer w = fWidths.get(Integer.toString(x));
        if (w == null)
            w = Integer.valueOf(value.length());
        else
            w = Integer.valueOf(Math.max(w.intValue(), value.length()));
        fWidths.put(Integer.toString(x), w);
        fRows = Math.max(fRows, y + 1);
    }

    private String getCell(int x, int y) {
        return fContent.get(x + "/" + y); //$NON-NLS-1$
    }

    public void print(PrintStream ps) {
        int n = fWidths.size();
        for (int y = 0; y < fRows; y++) {
            for (int x = 0; x < n; x++) {
                Integer w = fWidths.get(Integer.toString(x));
                int ww = w.intValue();
                String s = getCell(x, y);
                if (s == null)
                    s = ""; //$NON-NLS-1$

                if (x > 0)
                    for (int g = 0; g < fGap; g++)
                        ps.print(' ');

                int www = ww - s.length();
                String align = fAlignment.get(x + "/" + y); //$NON-NLS-1$
                if (LEFT.equalsIgnoreCase(align))
                    ps.print(s);
                for (int l = 0; l < www; l++)
                    ps.print(' ');
                if (RIGHT.equalsIgnoreCase(align))
                    ps.print(s);
            }
            ps.println();
        }
    }
}
